#include "stdafx.h"
#include "Form1.h"
#include "XchangeKey.h"
#pragma comment(lib, "logic.lib")

namespace KeyAss 
{
#pragma region Windows Form Designer generated code
	void Form1::InitializeComponent(void)
	{
		this->btnStopStart = (gcnew System::Windows::Forms::Button());
		this->Input = (gcnew System::Windows::Forms::TextBox());
		this->reload = (gcnew System::Windows::Forms::Button());
		this->SuspendLayout();
		// 
		// btnStopStart
		// 
		this->btnStopStart->Location = System::Drawing::Point(5, 7);
		this->btnStopStart->Name = L"btnStopStart";
		this->btnStopStart->Size = System::Drawing::Size(75, 23);
		this->btnStopStart->TabIndex = 0;
		this->btnStopStart->Text = L"To Pause";
		this->btnStopStart->UseVisualStyleBackColor = true;
		this->btnStopStart->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
		// 
		// Input
		// 
		this->Input->BackColor = System::Drawing::SystemColors::ButtonHighlight;
		this->Input->Location = System::Drawing::Point(5, 36);
		this->Input->Multiline = true;
		this->Input->Name = L"Input";
		this->Input->Size = System::Drawing::Size(208, 147);
		this->Input->TabIndex = 1;
		// 
		// reload
		// 
		this->reload->Location = System::Drawing::Point(172, 5);
		this->reload->Name = L"reload";
		this->reload->Size = System::Drawing::Size(29, 24);
		this->reload->TabIndex = 2;
		this->reload->Text = L"R";
		this->reload->UseVisualStyleBackColor = true;
		this->reload->Click += gcnew System::EventHandler(this, &Form1::reload_Click);
		// 
		// Form1
		// 
		this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
		this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
		this->ClientSize = System::Drawing::Size(219, 186);
		this->Controls->Add(this->reload);
		this->Controls->Add(this->Input);
		this->Controls->Add(this->btnStopStart);
		this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
		this->Name = L"Form1";
		this->Text = L"Working";
		this->ResumeLayout(false);
		this->PerformLayout();

	}
#pragma endregion

	static void SetShowing(const std::string& str)
	{
		Form1::get()->Input->Text = gcnew String(str.c_str());
	}

	static void MarshalString ( String^ s, std::string& os ) {
		using namespace Runtime::InteropServices;
		const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
		os = chars;
		Marshal::FreeHGlobal(IntPtr((void*)chars));
	}

	static const std::string& GetShowing()
	{
		static std::string text;
		MarshalString(Form1::get()->Input->Text, text);
		return text;
	}

	static void PauseResumeApp()
	{
		Form1::get()->PauseResume();
	}

	Form1::Form1(void)
	{
		_this = this;
		InitializeComponent();
		HWND texthwnd = (HWND)Form1::get()->Input->Handle.ToPointer();
		_X = new XchangeKey(SetShowing, GetShowing, PauseResumeApp, texthwnd);
		_X->Start();
	}

	Form1::~Form1()
	{
		if (components)
		{
			delete components;
		}
		_X->Stop();
	}

	void Form1::PauseResume()
	{
		if(_X->Paused()){
			_X->Resume();
			this->btnStopStart->Text = L"To Pause";
			this->Text = L"Working";
		}else{
			_X->Pause();
			this->btnStopStart->Text = L"To Resume";
			this->Text = L"Paused";
		}
	}

	System::Void Form1::button1_Click(System::Object^ , System::EventArgs^) 
	{
		PauseResume();
	}

	System::Void Form1::reload_Click(System::Object^  sender, System::EventArgs^  e) 
	{
		_X->ReloadCfg();
	}


}